package com.ssps.back.manage.user.service.impl;

import com.ssps.back.manage.user.service.Manage_UserService;
import com.ssps.entity.*;
import com.ssps.mapper.RoleMapper;
import com.ssps.mapper.UserInfoMapper;
import com.ssps.mapper.UserMapper;
import com.ssps.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class ManageUserServiceImpl implements Manage_UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public List<Role> selectUnassignRole(Integer uid) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        List<UserRoleKey> userRoleList = userRoleMapper.selectByExample(example);
        List<Integer> list = new ArrayList<>();
        for (UserRoleKey userRoleKey : userRoleList) {
            list.add(userRoleKey.getRid());
        }
        RoleExample example1 = new RoleExample();
        if (list.size() > 0) {
            RoleExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andRidNotIn(list);
        }
        return roleMapper.selectByExample(example1);
    }

    @Override
    public void delete(Integer uid, Integer[] rids2) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        List<Integer> list = Arrays.asList(rids2);
        criteria.andUidEqualTo(uid);
        criteria.andRidIn(list);
        userRoleMapper.deleteByExample(example);
    }

    @Override
    public void add(Integer uid, Integer[] rids) {
        for (Integer rid : rids) {
            UserRoleKey userRole = new UserRoleKey();
            userRole.setRid(rid);
            userRole.setUid(uid);
            userRoleMapper.insert(userRole);
        }
    }

    @Override
    public List<Role> selectassignRole(Integer uid) {
        UserRoleExample example1 = new UserRoleExample();
        UserRoleExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andUidEqualTo(uid);
        List<UserRoleKey> userRoleList = userRoleMapper.selectByExample(example1);
        List<Integer> list = new ArrayList<>();
        // 得到该用户所有的角色id
        for (UserRoleKey userRole : userRoleList) {
            list.add(userRole.getRid());
        }
        // 得到该用户已分配的角色
        List<Role> roleList = new ArrayList<>();
        RoleExample example2 = new RoleExample();
        if (list.size() > 0) {
            RoleExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andRidIn(list);
            roleList = roleMapper.selectByExample(example2);
        }
        return roleList;
    }

    @Override
    public List<UserInfo> selectByCondition(String condition, String rname) {
        UserInfoExample example = new UserInfoExample();
        if (!rname.equals("角色")) {
            RoleExample example1 = new RoleExample();
            RoleExample.Criteria criteria = example1.createCriteria();
            criteria.andRnameEqualTo(rname);
            List<Role> list = roleMapper.selectByExample(example1);

            UserRoleExample example2 = new UserRoleExample();
            UserRoleExample.Criteria criteria1 = example2.createCriteria();
            criteria1.andRidEqualTo(list.get(0).getRid());
            List<UserRoleKey> list1 = userRoleMapper.selectByExample(example2);
            if (list1.size() != 0) {
                List<Integer> list2 = new ArrayList<>();
                for (UserRoleKey userRole : list1) {
                    list2.add(userRole.getUid());
                }
                UserInfoExample.Criteria criteria2 = example.createCriteria();
                criteria2.andUidIn(list2).andUnameLike("%" + condition + "%");
                UserInfoExample.Criteria criteria3 = example.createCriteria();
                criteria3.andUidIn(list2).andIdCardLike("%" + condition + "%");
                example.or(criteria2);
                example.or(criteria3);
            } else {
                return new ArrayList<UserInfo>();
            }

        } else {
            example.or().andIdCardLike("%" + condition + "%");
            example.or().andUnameLike("%" + condition + "%");
        }
        example.setOrderByClause("uid desc");
        return userInfoMapper.selectByExample(example);
    }
}
